//
//  KYDiseaseDetailViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/15.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYDiseaseDetailViewController.h"
#import "KYDiseaseDetailModel.h"
#import <MJRefresh.h>

@interface KYDiseaseDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *modelList;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)NSInteger page;
@end
    static NSString *ID = @"diseaseDetailIdentifier";
@implementation KYDiseaseDetailViewController
-(NSMutableArray *)modelList{
    if (!_modelList) {
        _modelList = [NSMutableArray array];
    }
    return _modelList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc]init];
    self.view = tableView;
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    [self getdata];
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
-(void)refreshData{
    [self getdata];
}
-(void)loadMoreData{
    [self getdata];
}
-(void)getdata{
//    [self.tableView.mj_header beginRefreshing];
    if (self.tableView.contentOffset.y <= 0) {
        self.page ++;
    }else{
        self.page --;
        if (self.page < 0) {
            self.page = 0;
            [self.tableView.mj_footer endRefreshing];
            return;
        }
    }
    __weak typeof(self) weakSelf = self;
    [KYDiseaseDetailModel getDiseaseDetailModelSuccess:^(NSArray *diseaseDetail) {
        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, diseaseDetail.count)];
        [weakSelf.modelList insertObjects:diseaseDetail atIndexes:set];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    } withPage:self.page wihtDiseaseType:self.ci1];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    KYDiseaseDetailModel *model = self.modelList[indexPath.row];
    cell.textLabel.text = model.ci3_name;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KYDiseaseDetailModel *model = self.modelList[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectDiseaseDetail:diseaseDetail:)]) {
        [self.delegate didSelectDiseaseDetail:self diseaseDetail:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
